﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BTKERP.Model;
using BTKERP.BLL.Inventory;

namespace BTKERP.MVCWeb.Controllers
{
    public class ProductController : RestrictController
    {
        //
        // GET: /Product/

        public ActionResult Index()
        {
            List<Product> products = InventoryInfoMgt.GetProducts();
            ViewData["category"] = GetCategoryList();
            return View(products);
        }

        //
        // GET: /Product/Details/5

        public ActionResult Details(Guid id)
        {
            Product product = InventoryInfoMgt.GetProductById(id);
            return View(product);
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            Product product = new Product();
            ViewData["CategoryList"] = GetCategorySelectList();
            ViewData["UnitList"] = GetUnitSelectList();
            ViewData["SupplierList"] = GetSupplierSelectList();
            return View(product);
        } 

        //
        // POST: /Product/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Product product = new Product();
                UpdateModel(product);
                InventoryInfoMgt.AddProduct(product.Code, product.Name, product.PhysicalBarcode, product.PackBarcode, product.Description, product.CategoryId, product.PhysicalUnitId, product.PackUnitId, product.NumberPerPhysical, product.PhysicalSalePrice, product.PackSalePrice, SessionEntity.Staff.UserName, product.Status);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Product/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            Product product = InventoryInfoMgt.GetProductById(id);
            ViewData["CategoryList"] = GetCategorySelectList();
            ViewData["UnitList"] = GetUnitSelectList();
            ViewData["SupplierList"] = GetSupplierSelectList();
            return View(product);
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                Product product = new Product();
                UpdateModel(product);
                InventoryInfoMgt.UpdateProduct(product.Id, product.Code, product.Name, product.PhysicalBarcode, product.PackBarcode, product.Description, product.CategoryId, product.PhysicalUnitId, product.PackUnitId, product.NumberPerPhysical, product.PhysicalSalePrice, product.PackSalePrice, SessionEntity.Staff.UserName, product.Status);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Product/Delete/5
 
        public ActionResult Delete(Guid id)
        {
            Product product = InventoryInfoMgt.GetProductById(id);
            ViewData["CategoryList"] = GetCategorySelectList();
            ViewData["UnitList"] = GetUnitSelectList();
            ViewData["SupplierList"] = GetSupplierSelectList();
            return View(product);
        }

        //
        // POST: /Product/Delete/5

        [HttpPost]
        public ActionResult Delete(Product product)
        {
            try
            {
                // TODO: Add delete logic here
                bool isDeleted = InventoryInfoMgt.DeleteProduct(product.Id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
